package logica;

import entidades.Usuario;
//import marshals.MarshalArray;
//import marshals.MarshalDate;



import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.Marshal;
import org.ksoap2.serialization.MarshalDate;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.ksoap2.serialization.PropertyInfo;

public class LogicaUsuario implements ILogicaUsuario {

	private static LogicaUsuario instance = null; // variable para crear la
													// instancia de la logica
													// usuario

	public static LogicaUsuario getInstance() {

		if (instance == null) { // verifica si existe una instancia de la
								// logica, sino devuelve la creada anteriormente
			instance = new LogicaUsuario();
		} else {
			instance = new LogicaUsuario();
		}
		return instance;
	}

	private Object al;

	@Override
	public int AltaUsuario(Usuario usu){

		int resp = 0;
		try {

			/*
			 * WebServiceCentralService ws = new WebServiceCentralService();
			 * webservice.Usuario usuws = new webservice.Usuario();
			 * usuws.setNombre(usu.getNombre());
			 * usuws.setApellido(usu.getApellido());
			 * usuws.setDocumento(usu.getDocumento());
			 * usuws.setDireccion(usu.getDireccion());
			 * usuws.setEmail(usu.getEmail()); Date actual = new Date();
			 * GregorianCalendar fechaN = new GregorianCalendar();
			 * fechaN.setTime(actual); XMLGregorianCalendar fechaNac; fechaNac =
			 * DatatypeFactory.newInstance().newXMLGregorianCalendar(fechaN);
			 * usuws.setFechaNacimiento(fechaNac); usuws.setHabilitado(true);
			 * usuws.setPenalizado(false); usuws.setUsuario(usu.getUsuario());
			 * usuws.setClave(usu.getClave()); usuws.setTexto("");
			 * 
			 * 
			 * resp = ws.getWebServiceCentralPort().altaUsuario(usuws);
			 */

			String NAMESPACE = "http://webservice/";
			String URL = "http://192.168.1.41:8080/ServidorCentral/WebServiceCentral";
			String METHOD_NAME = "altaUsuario";
			String SOAP_ACTION = "http://webservice/altaUsuario";

			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

			PropertyInfo propInfo = new PropertyInfo();
			propInfo.setName("usuario");
			propInfo.setValue(usu);
			propInfo.setType(Usuario.class);
			request.addProperty(propInfo);

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.setOutputSoapObject(request);
			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

			androidHttpTransport.call(SOAP_ACTION, envelope);

			SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope
					.getResponse();

			String resps = resultsRequestSOAP.toString();
			resp = Integer.parseInt(resps);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return resp;
	}

	@Override
	public Usuario BuscarUsuarioLogueo(Usuario usu) {
		// TODO Auto-generated method stub
		Usuario resp = null;
		String envio ="";
		String recepcion ="";
		try
		{
			String NAMESPACE = "http://webservice/";
			//String URL = "http://t3104862:8080/ServidorCentral/WebServiceCentral?wsdl";
			//String URL = "http://10.37.49.175:8080/ServidorCentral/WebServiceCentral";
			String URL = "http://192.168.1.41:8080/ServidorCentral/WebServiceCentral";
			String METHOD_NAME = "buscarUsuarioLogueo";
			String SOAP_ACTION = "http://webservice/buscarUsuarioLogueo";
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

			PropertyInfo propInfo = new PropertyInfo();
			propInfo.setName("arg0");
			propInfo.setValue(usu);
			propInfo.setType(Usuario.class);
			request.addProperty(propInfo);
			

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER10);

			envelope.setOutputSoapObject(request);
			envelope.addMapping(NAMESPACE, "usuario", new Usuario().getClass());
			envelope.setAddAdornments(false);
			envelope.implicitTypes = true;
			
			Marshal md = new MarshalDate();
			//MarshalDate md = new MarshalDate();
			/*MarshalArray al = new MarshalArray();
			 
			al.register(envelope);*/
			md.register(envelope);
			
			
		    

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			androidHttpTransport.debug = true;
			androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			androidHttpTransport.call(SOAP_ACTION, envelope);
			envio=androidHttpTransport.requestDump;
			recepcion=androidHttpTransport.responseDump;
			//SoapPrimitive resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
			SoapObject resultsRequestSOAP = (SoapObject) envelope.getResponse();
			if(resultsRequestSOAP != null){
			resp = new Usuario();
			/*resp.setNombre((String) resultsRequestSOAP.getProperty("Nombre"));
			resp.setApellido((String) resultsRequestSOAP.getProperty("Apellido"));
			resp.setDireccion((String) resultsRequestSOAP.getProperty("Direccion"));
			resp.setDocumento((Long)resultsRequestSOAP.getProperty("Documento"));
			resp.setEmail((String) resultsRequestSOAP.getProperty("Email"));*/
			resp.setUsuario(resultsRequestSOAP.getPropertyAsString("usuario"));
			resp.setClave(resultsRequestSOAP.getPropertyAsString("clave"));
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resp;
	}

	@Override
	public int Suma(int op1, int op2) {
		int resp =0;
		String envio = "";
		String recepcion ="";
		try
		{
			String NAMESPACE = "http://webservice/";
			//String URL = "http://t3104862:8080/ServidorCentral/WebServiceCentral?wsdl";
			String URL = "http://10.37.49.175:8080/ServidorCentral/WebServiceCentral?wsdl";
			//String URL = "http://192.168.1.41:8080/ServidorCentral/WebServiceCentral?wsdl";
			String METHOD_NAME = "suma";
			String SOAP_ACTION = "http://webservice/suma";
			SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

			PropertyInfo infoOp1 = new PropertyInfo();
			infoOp1.setName("op1");
			infoOp1.setValue(op1);
			infoOp1.setType(Integer.class);
			PropertyInfo infoOp2 = new PropertyInfo();
			infoOp2.setName("op2");
			infoOp2.setValue(op2);
			infoOp2.setType(Integer.class);
			
			
			request.addProperty(infoOp1);
			request.addProperty(infoOp2);
			

			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

			envelope.setOutputSoapObject(request);
			//envelope.addMapping(NAMESPACE, "op1", new Usuario().getClass());
			//envelope.setAddAdornments(false);
			//envelope.implicitTypes = true;
			
			//Marshal md = new MarshalDate();
			//MarshalDate md = new MarshalDate();
			/*MarshalArray al = new MarshalArray();
			 
			al.register(envelope);*/
			//md.register(envelope);
			
			
		    

			HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
			androidHttpTransport.debug = true;
			androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			androidHttpTransport.call(SOAP_ACTION, envelope);
			envio=androidHttpTransport.requestDump;
			recepcion=androidHttpTransport.responseDump;
			SoapObject response = (SoapObject)envelope.getResponse();
            resp =  Integer.parseInt(response.getProperty(0).toString());
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return resp;
	}

	
	
}

